package tx.huobi.ws.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import tx.huobi.tools.SendmailUtil;
import tx.huobi.ws.IWebSocketService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WebSocketService implements IWebSocketService{
	private Logger log = Logger.getLogger(WebSocketService.class);
	public static int btc=0;
	public static double eos=0;
	public static double ada=0;
	public static int bch=0;

	public void onReceive(JSONObject json) {
		//log.info("receive:" + json.toJSONString());
		try {
			if (json.toJSONString().length()>50){
				String ts = json.getString("ts");
				String chStr = json.getString("ch");
				String bi = StringUtils.substring(chStr, chStr.indexOf(".") + 1, chStr.indexOf(".kline")).replace("usdt", "");
				String tick = json.getString("tick");
				JSONObject json2 = JSONObject.parseObject(tick);
				String close = json2.getString("close");
				Date date = new Date(Long.valueOf(ts));
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//log.info("----:"+format.format(date)+"---"+bi+"----:"+close.substring(0,8));
				System.out.println(format.format(date) + "---" + bi + "----:" + close.substring(0, 8));
				if ("btc".equals(bi)){
					int price_now=Double.valueOf(close).intValue();
					if (price_now-btc>30){
						SendmailUtil.mail("btc上升30$，当前价:"+close.substring(0, 8)+"");
						btc=price_now;
					}else if (btc-price_now>30){
						btc=price_now;
						SendmailUtil.mail("btc下降30$，当前价:"+close.substring(0, 8)+"");
					}
				}
				if ("eos".equals(bi)){
					double price_now=Double.valueOf(close);
					if (price_now-eos>0.5){
						SendmailUtil.mail("eos上升0.5$，当前价:"+close.substring(0, 8)+"");
						eos=price_now;
					}else if (eos-price_now>0.5){
						eos=price_now;
						SendmailUtil.mail("eos下降0.5$，当前价:"+close.substring(0, 8)+"");
					}
				}
				if ("ada".equals(bi)){
					double price_now=Double.valueOf(close);
					if (price_now-ada>0.005){
						SendmailUtil.mail("ada上升0.005$，当前价:"+close.substring(0, 8)+"");
						ada=price_now;
					}else if (ada-price_now>0.005){
						ada=price_now;
						SendmailUtil.mail("ada下降0.005$，当前价:"+close.substring(0, 8)+"");
					}
				}
				if ("bch".equals(bi)){
					int price_now=Double.valueOf(close).intValue();
					if (price_now-bch>20){
						SendmailUtil.mail("bch上升20$，当前价:"+close.substring(0, 8)+"");
						bch=price_now;
					}else if (bch-price_now>20){
						bch=price_now;
						SendmailUtil.mail("bch下降20$，当前价:"+close.substring(0, 8)+"");
					}
				}
			}else{
				//log.info("receive:" + json.toJSONString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void onReset(){
		
	}
}
